﻿using System;
using System.Web.Mvc;
using lindyaka.Models.Administration;
using lindyaka.Services.Administration;

namespace lindyaka.Web.Areas.Administration.Controllers
{
    public class UserAccountsController : Controller
    {
        private readonly UserAccounts _userAccounts;

        public UserAccountsController()
        {
            _userAccounts = new UserAccounts();
        }

        //
        // GET: /Administration/UserAccounts/

        public ActionResult Index()
        {
            return View(_userAccounts.Get());
        }

        //
        // GET: /Administration/UserAccounts/Details/5

        public ActionResult Details(int id)
        {
            return View(_userAccounts.Get(id));
        }

        //
        // GET: /Administration/UserAccounts/Create

        public ActionResult Create()
        {
            return View(new UserAccount
                {
                    Registered = DateTime.UtcNow,
                    LastLoggedIn = DateTime.UtcNow
                });
        }

        //
        // POST: /Administration/UserAccounts/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            var userAccount = new UserAccount();
            TryUpdateModel(userAccount);
            if (ModelState.IsValid)
            {
                _userAccounts.Add(userAccount);
                return RedirectToAction("Index");
            }
            return View(userAccount);
        }

        //
        // GET: /Administration/UserAccounts/Edit/5

        public ActionResult Edit(int id)
        {
            return View(_userAccounts.Get(id));
        }

        //
        // POST: /Administration/UserAccounts/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            UserAccount userAccount = _userAccounts.Get(id);
            TryUpdateModel(userAccount);
            if (ModelState.IsValid)
            {
                _userAccounts.Update(userAccount);
                return RedirectToAction("Index");
            }
            return View(userAccount);
        }

        //
        // GET: /Administration/UserAccounts/Delete/5

        public ActionResult Delete(int id)
        {
            return View(_userAccounts.Get(id));
        }

        //
        // POST: /Administration/UserAccounts/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            UserAccount userAccount = _userAccounts.Get(id);
            try
            {
                _userAccounts.Remove(userAccount);
                return RedirectToAction("Index");
            }
            catch
            {
                return View(userAccount);
            }
        }
    }
}